package com.example.contrller.admin;

import com.example.biz.AppInfo.AppInfobiz;
import com.example.biz.BacenUser.IBacenUserbiz;
import com.example.biz.DevUser.IDevUserbiz;
import com.example.pojo.AppInfo;
import com.example.pojo.BackendUser;
import com.example.pojo.DevUser;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BackenUserController {
    @Resource
    private AppInfobiz bizll;
    @Resource
    private IBacenUserbiz biz;
    @Resource
    private IDevUserbiz biz1;

    @PostMapping("backenUser")
    public String Login(String userCode, String userPassword, HttpSession session) {
        BackendUser user = biz.findBackenUser(userCode);
        if (user == null || !user.getUserPassword().equals(userPassword)) {
            return "admin/login";
        }
        session.setAttribute("backenUser", user);
        return "admin/main";
    }

    @RequestMapping("infoMo")
    public String  findAppInfoMo(AppInfo appInfo,Model model){
        List<AppInfo> list=bizll.findAppInfoMo(appInfo);
        model.addAttribute("page",list);
        return "admin/Audit";
    }


    @RequestMapping("LogOut")
    public String LogOut(HttpSession session) {
        session.removeAttribute("backenUser");
        return "admin/login";
    }

    @GetMapping("Look/{userCode}")
    public String Look(@PathVariable(name = "userCode") String userCode, HttpSession session, Model model) {
        List<DevUser> list = biz1.findDevUserAll();
        BackendUser user = biz.findBackenUser(userCode);

        session.setAttribute("backenUser", user);
        model.addAttribute("page", list);
        return "admin/main01";
    }

    @RequestMapping("dele/{id}")
    public String dele(@PathVariable(name = "id") Integer id, Model model) {
        List<DevUser> list = biz1.findDevUserAll();
        model.addAttribute("page", list);
        biz1.findDelete(id);
        return "redirect:/admin/Look/admin";
    }

    @RequestMapping("DevUserid/{id}")
    public String findDevUserid(@PathVariable(name = "id") Integer id, HttpSession session) {
        DevUser devUser = biz1.findDevUserid(id);
        session.setAttribute("devUser", devUser);
        return "admin/Amend";
    }

    @GetMapping("findDevUpdate")
    public String findDevUserUpdate(DevUser user) {
        biz1.findDevUserUpdate(user);
        return "redirect:/admin/Look/admin";
    }

    @PostMapping("findDevUserInsert")
    public String findDevAdd(DevUser user) {
        biz1.findDevUserInsert(user);
        return "redirect:/admin/Look/admin";
    }

    @GetMapping("DevUserAdd/{userCode}")
    public String DevAdd(@PathVariable(name = "userCode") String userCode, HttpSession session, Model model) {
        List<DevUser> list = biz1.findDevUserAll();
        BackendUser user = biz.findBackenUser(userCode);

        session.setAttribute("backenUser", user);
        model.addAttribute("page", list);
        return "admin/AddUser";
    }

    @RequestMapping("findBackShow")
    public String findBackUserShow(Model model){
        List<BackendUser> list=biz.findBackUserShow();
        model.addAttribute("page",list);
         return "admin/ManagEment";
    }

    @RequestMapping("infoShow")
    public String findInfo(AppInfo appInfo,Model model){
        List<AppInfo> list=bizll.findUserId(appInfo);
        model.addAttribute("page",list);
        return "admin/Audit";
    }

}